package com.gomore.opple.module.redpackage.initial;

import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;

import com.gomore.opple.R;
import com.gomore.opple.common.GlobalConstant;
import com.gomore.opple.model.EventPhoto;
import com.gomore.opple.module.BaseFragmentV4;
import com.gomore.opple.module.redpackage.adapter.RedPackageAdapter;
import com.gomore.opple.utils.DialogUtils;
import com.gomore.opple.utils.PhotoUtils;
import com.gomore.opple.widgets.PullBaseView;
import com.gomore.opple.widgets.PullRecyclerView;

import java.io.File;

import butterknife.Bind;
import de.greenrobot.event.EventBus;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;


/**
 * Created by robot
 */
public class InitialFragment extends BaseFragmentV4 implements InitialContract.View,
        PullBaseView.OnRefreshListener {

    @Bind(R.id.recyclerView)
    PullRecyclerView recyclerView;
    @Bind(R.id.empty_view)
    View empty_view;
    private InitialContract.Presenter mPresenter;
    private RedPackageAdapter redPackageAdapter;


    public static InitialFragment getInstance() {
        return new InitialFragment();
    }

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_initial;
    }

    @Override
    protected void initalizeData() {
        EventBus.getDefault().register(this);
        mPresenter.queryRedPackage(false);
    }

    @Override
    protected void initalizeViews() {
        redPackageAdapter = new RedPackageAdapter(getActivity(), getActivity(), R.layout.item_red_package, mPresenter.getData(), GlobalConstant.RedPackageState.INITIAL);
        recyclerView.setLayoutManager(new LinearLayoutManager(InitialFragment.this.getActivity()));
        recyclerView.setAdapter(redPackageAdapter);
        recyclerView.setOnRefreshListener(this);
    }


    @Override
    public void setPresenter(InitialContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void showProgressDialog() {
        DialogUtils.showLoadingDialog(getActivity(), getResources().getString(R.string.loading));
    }

    @Override
    public void hideProgressDialog() {
        DialogUtils.closeLoadingDialog();
    }

    @Override
    public void showMessage(String msg) {
        showToast(msg);
    }

    @Override
    public void showMessage(int msgId) {
        showToast(msgId);
    }

    /**
     * 加载完毕
     */
    @Override
    public void showLoadMoreCompleted() {
        recyclerView.onFooterRefreshComplete();
    }

    /**
     * 刷新完毕
     */
    @Override
    public void showRefreshCompleted() {
        recyclerView.onHeaderRefreshComplete();
    }

    @Override
    public void showContentView() {
        redPackageAdapter.notifyDataSetChanged();
        if (mPresenter.getData().size() == 0) {
            empty_view.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        } else {
            empty_view.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onHeaderRefresh(PullBaseView view) {
        mPresenter.queryRedPackage(false);
    }

    @Override
    public void onFooterRefresh(PullBaseView view) {
        mPresenter.queryRedPackage(true);
    }

    public void onEventMainThread(final EventPhoto eventPhoto) {
        if (eventPhoto != null && eventPhoto.getFragmentType() != null &&
                eventPhoto.getUrl() != null) {
            if (eventPhoto.getFragmentType().equals(GlobalConstant.FragmentType.INITIALFRAGMENT)) {
                final int position = eventPhoto.getPosition();
                //先显示弹框
                showProgressDialog();
                //压缩图片,上传图片
                new Thread(){
                    @Override
                    public void run() {
                        PhotoUtils.getCompressPhoto(eventPhoto.getUrl(), "2");
                        //上传图片
                        File file = new File(eventPhoto.getUrl());
                        RequestBody requestFile = RequestBody.create(MediaType.parse("image/png"), file);
                        MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
                        mPresenter.saveAttachment(body, position);
                    }
                }.start();
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
